package com.cuiweiyou.contactsfromfile;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by macpro on 2018/3/3.
 */

public class ContactAdapter extends BaseAdapter {
    Context context;
    List<String> list;
    List<String> injected;

    public ContactAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
        this.injected = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return null == list ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item, null);
            holder = new Holder();
            holder.textView = convertView.findViewById(R.id.textView);
            holder.imageView = convertView.findViewById(R.id.imageView);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        String contactBean = list.get(position);
        holder.textView.setText(contactBean);
        if (injected.contains(contactBean)) {
            holder.imageView.setImageResource(R.mipmap.pre);
        } else {
            holder.imageView.setImageResource(R.mipmap.nor);
        }

        return convertView;
    }

    public void setData(List<String> list) {
        if (null != this.list) {
            this.list.clear();
            this.list = null;
        }
        this.injected.clear();
        this.list = list;
        notifyDataSetChanged();
    }

    public List<String> getData() {
        return list;
    }

    public void addInject(String bean) {
        this.injected.add(bean);
        notifyDataSetChanged();
    }

    public List<String> getInjected() {
        return injected;
    }

    class Holder {
        TextView textView;
        ImageView imageView;
    }
}
